package com.abc.fast.common.domain.vo.resp;

import com.abc.fast.common.constant.HttpStatus;
import com.abc.fast.common.domain.enums.CommonErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 通用返回体
 * @Author LiJunXi
 * @Date 2024-03-01 14:26
 */
@Data
@ApiModel("基础返回体")
public class ApiResult<T> {
    @ApiModelProperty("状态码")
    private Integer code;
    @ApiModelProperty("错误消息")
    private String msg;
    @ApiModelProperty("返回对象")
    private T data;

    public static <T> ApiResult<T> success() {
        ApiResult<T> result = new ApiResult<T>();
        result.setData(null);
        result.setCode(HttpStatus.SUCCESS);
        return result;
    }

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = new ApiResult<T>();
        result.setData(data);
        result.setCode(HttpStatus.SUCCESS);
        return result;
    }

    public static <T> ApiResult<T> fail(String msg) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(HttpStatus.ERROR);
        result.setMsg(msg);
        return result;
    }

    public static <T> ApiResult<T> fail(CommonErrorEnum commonErrorEnum) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(commonErrorEnum.getCode());
        result.setMsg(commonErrorEnum.getDesc());
        return result;
    }

    public static <T> ApiResult<T> fail(Integer code, String msg) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}
